package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.NotifyListChanged;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class MessageItem extends BindingListItem {
    private int id;
    private String content;
    private Date msgtime;
    private String title;
    private NotifyListChanged parent;

    public NotifyListChanged getParent() {
        return parent;
    }

    public void setParent(NotifyListChanged parent) {
        this.parent = parent;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
        notifyPropertyChanged(BR.content);
    }

    @Bindable
    public Date getMsgtime() {
        return msgtime;
    }

    public void setMsgtime(Date msgtime) {
        this.msgtime = msgtime;
        notifyPropertyChanged(BR.msgtime);
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }

    public MessageItem(Context context) {
        super(context, R.layout.i_my_message);
    }

    /**
     * 点击消息，进入消息详情
     *
     * @param view
     */
    public void onClickItem(View view) {
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.readNews(getContext(), getId())) {
//                    if (parent != null) {
//                        parent.notifyListChanged(ListBindingAdapter.ACTION_DEL, MessageItem.this);
//                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HomeActivity.notifyListChange(ListBindingAdapter.ACTION_DEL, MessageItem.this);
                            MessageDetailModel model = new MessageDetailModel(getContext());
                            model.setMsgtime(getMsgtime());
                            model.setTitle(getTitle());
                            model.setContent(getContent());
                            model.setId(getId());
                            CommonFragment fragment = new CommonFragment();
                            fragment.setVariableId(BR.message);
                            fragment.setLayoutId(R.layout.f_message_view);
                            fragment.setDataModel(model);
                            HomeActivity.switchFragment((Activity) getContext(), fragment);
                        }
                    });
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }
}
